# Maintainer: Marian Buschsieweke <marian.buschsieweke@ovgu.de>
# Contributor: Marian Buschsieweke <marian.buschsieweke@ovgu.de>
pkgname=kicad
pkgver=5.1.5
pkgrel=4
pkgdesc="A Cross Platform and Open Source Electronics Design Automation Suite"
url="http://kicad-pcb.org/"
arch="all !s390x !mips !mips64" # No support for s390x in libcontext (shipped with kicad) broken on mips
license="GPL-3.0-or-later"
makedepends="
	boost-dev
	cairo-dev
	cmake
	curl-dev
	glew-dev
	glm-dev
	ngspice-dev
	py3-wxpython
	python3-dev
	swig
	wxgtk-base-dev
	wxgtk3-dev
	zlib-dev
	"
depends="python3"
subpackages="$pkgname-i18n:i18n:noarch $pkgname-doc"
source="
	https://launchpad.net/kicad/5.0/$pkgver/+download/kicad-$pkgver.tar.xz
	kicad-i18n.$pkgver.tar.gz::https://github.com/KiCad/kicad-i18n/archive/$pkgver.tar.gz
	"

case "$CARCH" in
	x86_64)
		makedepends="$makedepends opencascade-dev vtk-dev"
		subpackages="$subpackages kicad-opencascade"
		;;
	*)
		# Dependency opencascade (needed for STEP file support in 3D preview)
		# not available on all other architectures
		;;
esac

_build_kicad() {
	mkdir -p "$1"
	cd "$1"
	cmake .. \
		-DCMAKE_BUILD_TYPE=None \
		-DCMAKE_INSTALL_PREFIX=/usr \
		-DCMAKE_INSTALL_LIBDIR=lib \
		-DKICAD_USE_OCE=OFF \
		-DKICAD_USE_OCC=$2 \
		-DKICAD_SCRIPTING=ON \
		-DKICAD_SCRIPTING_PYTHON3=ON  \
		-DKICAD_SCRIPTING_MODULES=ON  \
		-DKICAD_SCRIPTING_WXPYTHON=ON \
		-DKICAD_SCRIPTING_ACTION_MENU=ON \
		-DKICAD_SCRIPTING_WXPYTHON_PHOENIX=ON \
		-DwxWidgets_CONFIG_EXECUTABLE=/usr/bin/wx-config-gtk3 \
		-DwxWidgets_INCLUDE_DIRS=/usr/include \
		-DBUILD_GITHUB_PLUGIN=ON
	make
	cd "$builddir"
}

_build_kicad_i18n() {
	cd "$srcdir"/kicad-i18n-$pkgver
	mkdir -p build
	cd build
	cmake .. -DCMAKE_INSTALL_PREFIX=/usr
	make
	cd "$builddir"
}

build() {
	# First build without opencascade
	_build_kicad "build" "OFF"
	_build_kicad_i18n

	# Now build with opencascade, if possible
	case "$CARCH" in
		x86_64)
			_build_kicad "build-occ" "ON"
			;;
		*)
			# Dependency opencascade (needed for STEP file support in 3D preview)
			# not available on all other architectures
			;;
	esac
}

check() {
	cd build
	make test
}

package() {
	cd build
	make DESTDIR="$pkgdir" install
}

i18n() {
	cd "$srcdir"/kicad-i18n-$pkgver/build
	make DESTDIR="$subpkgdir" install
}

opencascade() {
	pkgdesc="kicad with support for STEP file in 3D preview"
	replaces="$pkgname"
	provides="$pkgname=$pkgver-r$pkgrel"
	cd "$builddir/build-occ"
	make DESTDIR="$subpkgdir" install
}

sha512sums="5ca19e219da07a906f13e236849eb4497c9f827726a5cd13aceffe1d8c7dee480823bf3e949527f59a805eb3b8176fad51576ae663dd07b10e2822f2b37866a5  kicad-5.1.5.tar.xz
68e6602fcc7e73c22a594b45ecf53a956af8a134e28e86242bfaa4087df21c04b24ae4df9928f00c2cee7eb0631a02a4de1cd09547b36a466fb324f983ea5363  kicad-i18n.5.1.5.tar.gz"
